using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Windows.Controls.Primitives;
using Microsoft.Surface.Presentation.Controls.Primitives;

namespace LetsGoToSchool
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class Main : SurfaceWindow
    {
        private Database _database = new Database();
        private static string _currentUser;
        public static string CurrentUser { get { return _currentUser; } }
        public static ProgressDataNode _node;



        /// <summary>
        /// Default constructor.
        /// </summary>
        public Main()
        {
            InitializeComponent();

            // Add handlers for Application activation events
            AddActivationHandlers();
            ParentControl.setMain(this);
            //Contacts.AddPreviewContactDownHandler(ParentSlider, ParentSlider_ContactDown);

            _login.setMain(this);

            _doodleGameButton.Background = new ImageBrush(Utils.getIcon(Doodle.getIconFileName()));
            _memoryGameButton.Background = new ImageBrush(Utils.getIcon(Memory.getIconFileName()));
            _musicGameButton.Background = new ImageBrush(Utils.getIcon(MusicGame.getIconFileName()));

           
            

           /* MemorySessionNode mem_e = new MemorySessionNode();
            mem_e.setDifficulty(0);
            mem_e.setMoves(30);
            mem_e.setTime(TimeSpan.FromMinutes(30));
            MemorySessionNode mem_m = new MemorySessionNode();
            mem_m.setDifficulty(1);
            mem_m.setMoves(50);
            mem_m.setTime(TimeSpan.FromMinutes(50));
            MemorySessionNode mem_h = new MemorySessionNode();
            mem_h.setDifficulty(2);
            mem_h.setMoves(70);
            mem_h.setTime(TimeSpan.FromMinutes(70));

            MusicSessionNode mus_e = new MusicSessionNode();
            mus_e.setDifficulty(0);
            mus_e.setPercentCorrect(90);
            mus_e.setSong("Old MacDonald");
            mus_e.setTime(TimeSpan.FromMinutes(10));
            MusicSessionNode mus_m = new MusicSessionNode();
            mus_m.setDifficulty(0);
            mus_m.setPercentCorrect(90);
            mus_m.setSong("Old MacDonald");
            mus_m.setTime(TimeSpan.FromMinutes(10));
            MusicSessionNode mus_h = new MusicSessionNode();
            mus_h.setDifficulty(0);
            mus_h.setPercentCorrect(90);
            mus_h.setSong("Old MacDonald");
            mus_h.setTime(TimeSpan.FromMinutes(10));

            Database.writeMemorySessionData(mem_e);
            Database.writeMemorySessionData(mem_m);
            Database.writeMemorySessionData(mem_h);
            Database.writeMusicSessionData(mus_e);
            Database.writeMusicSessionData(mus_m);
            Database.writeMusicSessionData(mus_h);*/


           // _currentUser = "james";
            //ProgressDataNode n = Database.generateReport("James");
            //n.printData();
            /*MemorySessionNode mem1 = new MemorySessionNode();
            mem1.setDifficulty(1);
            mem1.setMoves(10);
            mem1.setTime(TimeSpan.FromMinutes(10));
            MemorySessionNode mem2 = new MemorySessionNode();
            mem2.setDifficulty(1);
            mem2.setMoves(20);
            mem2.setTime(TimeSpan.FromMinutes(10));
            MemorySessionNode mem3 = new MemorySessionNode();
            mem3.setDifficulty(1);
            mem3.setMoves(4);
            mem3.setTime(TimeSpan.FromMinutes(5));
            MemorySessionNode mem4 = new MemorySessionNode();
            mem4.setDifficulty(1);
            mem4.setMoves(12);
            mem4.setTime(TimeSpan.FromMinutes(8));

            MusicSessionNode mus1 = new MusicSessionNode();
            mus1.setDifficulty(1);
            mus1.setPercentCorrect(75.0);
            mus1.setSong("old macdonald");
            mus1.setTime(TimeSpan.FromMinutes(3));
            MusicSessionNode mus2 = new MusicSessionNode();
            mus2.setDifficulty(1);
            mus2.setPercentCorrect(20.0);
            mus2.setSong("old siffat");
            mus2.setTime(TimeSpan.FromMinutes(.5));
            MusicSessionNode mus3 = new MusicSessionNode();
            mus3.setDifficulty(1);
            mus3.setPercentCorrect(90.0);
            mus3.setSong("old yudi");
            mus3.setTime(TimeSpan.FromMinutes(10));
            MusicSessionNode mus4 = new MusicSessionNode();
            mus4.setDifficulty(1);
            mus4.setPercentCorrect(50.0);
            mus4.setSong("old mhannah");
            mus4.setTime(TimeSpan.FromMinutes(5));

            DoodleSessionNode dood1 = new DoodleSessionNode();
            dood1.setTime(TimeSpan.FromMinutes(50));

            Database.writeMemorySessionData(mem1);
            Database.writeMemorySessionData(mem2);
            Database.writeMemorySessionData(mem3);
            Database.writeMemorySessionData(mem4);
            Database.writeMusicSessionData(mus1);
            Database.writeMusicSessionData(mus2);
            Database.writeMusicSessionData(mus3);
            Database.writeMusicSessionData(mus4);
            Database.writeDoodleSessionData(dood1);*/



            
        }

        private void _helpButton_Click(object sender, RoutedEventArgs e)
        {
            NoContactCanvas.Visibility = Visibility.Visible;
            //I didn't check in the video, just comment the line out
            _helpBox.playMovie("C:/lgts-Images/kung-fu-panda-trailer.mpg");
        }

        private void _quitButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void _aboutButton_Click(object sender, RoutedEventArgs e)
        {
            _about.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// end the corrent session and go back to image selection mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _restarteButton_Click(object sender, RoutedEventArgs e)
        {
            ParentControl.Hide();
            _login.Visibility = Visibility.Visible;
            _grid.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        private Boolean _isScrollBarContactDown = false;
        private void ParentSlider_ContactDown(object sender, ContactEventArgs e)
        {
            _isScrollBarContactDown = true;
        }

        private void ParentSlider_ContactUp(object sender, ContactEventArgs e)
        {
            if (_isScrollBarContactDown)
            {
                SurfaceScrollBar slider = (sender as SurfaceScrollBar);
                if (slider.Value == slider.Maximum)
                {
                    ParentControl.Show();
                    _sliderbg.Visibility = Visibility.Hidden;
                    ParentSlider.Visibility = Visibility.Hidden;
                }
                ParentSlider.Value = ParentSlider.Minimum;
            }
            _isScrollBarContactDown = false;
        }

        public void ParentControlHidden()
        {
            _sliderbg.Visibility = Visibility.Visible;
            ParentSlider.Visibility = Visibility.Visible;
        }

        private void _memoryGameButton_Click(object sender, RoutedEventArgs e)
        {
            (new MemoryGame()).Show();
        }

        private void _doodleGameButton_Click(object sender, RoutedEventArgs e)
        {
            (new Doodle()).Show();
        }

        private void _musicGame_Click(object sender, RoutedEventArgs e)
        {
            (new MusicGame()).Show();
        }

        public void display()
        {
            MainCanvas.Visibility = Visibility.Visible;
            _login.Visibility = Visibility.Hidden;
            _grid.Visibility = Visibility.Visible;
            ParentControl.Visibility = Visibility.Hidden;

        }

        public void setCurrentUser(string user)
        {
            _currentUser = user;
        }

    }
}